<?php
class Admin_Model_DbTable_Admin extends Zend_Db_Table_Abstract {
	protected $_name = 'admin';
	public function addAdmin($role_id, $name, $description, $dateon, $dateoff, $img_profile, $status) {
		$data = array (
				'ROLE_ID' => $role_id,
				'NAME' => $name,
				'DESCRIPTION' => $description,
				'DATEON' => $dateon,
				'DATEOFF' => $dateoff,
				'IMG_PROFILE' => $img_profile,
				'STATUS' => $status 
		);
		return $this->insert ( $data );
	}
	public function updateAdmin($id, $role_id, $name, $description, $dateon, $dateoff, $img_profile, $status) {
		$data = array (
				'ROLE_ID' => $role_id,
				'NAME' => $name,
				'DESCRIPTION' => $description,
				'DATEON' => $dateon,
				'DATEOFF' => $dateoff,
				'IMG_PROFILE' => $img_profile,
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function updateStatus($id, $status) {
		$data = array (
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function updateRole($id, $role_id) {
		$data = array (
				'ROLE_ID' => $role_id 
		);
		return $this->update ( $data, "ROLE_ID = $role_id" );
	}
	public function removeAdmin($id) {
		return $this->delete ( "ID = $id" );
	}
	public function getAllAdmin() {
		return $this->fetchAll ( "STATUS = 1" )->toArray ();
	}
	public function getAdminById($id) {
		return $this->fetchRow("ID = $id");
	}
	public function getAdminByRoleId($role_id, $status) {
		
		$select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
						->setIntegrityCheck(false);
		
		$rows = NULL;
		
		$select
				->from("admin AS ad", array("ad.ID","ad.NAME","ad.DESCRIPTION"))
				->where("ad.STATUS = ?",$status)
				->join("role AS r", 
						"r.ID = ad.ROLE_ID",array("r.ROLE_NAME"))
				->where("r.ID = ?",$role_id);
		try {
			$rows = $this->fetchAll($select);
		} catch (Exception $e) {
			print_r($e);
		}		
		
		return $rows->toArray();
		
	}
	public function getAdminByName($admin_name, $status) {
		return $this->fetchRow("NAME = '$admin_name' AND STATUS = $status")->toArray();
	}
	public function getAdminByStatus($status) {
		return $this->fetchAll("STATUS = $status")->toArray();
	}
}
